package com.huawei.demo;

// 用户身份认证
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
// 请求异常类
import com.huaweicloud.sdk.core.exception.ClientRequestException;
// 导入待请求接口的 request 和 response 类
import com.huaweicloud.sdk.organizations.v1.OrganizationsClient;
import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationRequest;
import com.huaweicloud.sdk.organizations.v1.model.CreateOrganizationResponse;
import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationRequest;
import com.huaweicloud.sdk.organizations.v1.model.DeleteOrganizationResponse;
import com.huaweicloud.sdk.organizations.v1.model.ListRootsRequest;
import com.huaweicloud.sdk.organizations.v1.model.ListRootsResponse;
import com.huaweicloud.sdk.organizations.v1.model.ListTagsForResourceRequest;
import com.huaweicloud.sdk.organizations.v1.model.ListTagsForResourceResponse;
import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationRequest;
import com.huaweicloud.sdk.organizations.v1.model.ShowOrganizationResponse;
import com.huaweicloud.sdk.organizations.v1.model.TagDto;
import com.huaweicloud.sdk.organizations.v1.model.TagResourceReqBody;
import com.huaweicloud.sdk.organizations.v1.model.TagResourceRequest;
import com.huaweicloud.sdk.organizations.v1.model.TagResourceResponse;
import com.huaweicloud.sdk.organizations.v1.model.UntagResourceReqBody;
import com.huaweicloud.sdk.organizations.v1.model.UntagResourceRequest;
import com.huaweicloud.sdk.organizations.v1.model.UntagResourceResponse;
// 日志打印
import com.huaweicloud.sdk.organizations.v1.region.OrganizationsRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

public class OrganizationsTagManagementDemo {
    private static final Logger logger = LoggerFactory.getLogger(OrganizationsTagManagementDemo.class.getName());

    public static void main(String[] args) {
        // 创建认证
        String ak = "{your ak string}";
        String sk = "{your sk string}";

        ICredential auth = new GlobalCredentials().withAk(ak).withSk(sk);

        OrganizationsClient organizationsClient =
            OrganizationsClient.newBuilder()
                .withCredential(auth)
                .withRegion(OrganizationsRegion.valueOf("cn-north-4"))
                .build();

        OrganizationsTagManagementDemo demo = new OrganizationsTagManagementDemo();

        // 1、创建组织
        demo.createOrganization(organizationsClient);
        // 2、查询所属组织信息
        demo.showOrganization(organizationsClient);
        // 3、列出组织的根
        ListRootsResponse listRootsResponse = demo.listRoots(organizationsClient);
        // 4、为指定资源添加标签
        demo.tagResource(organizationsClient, listRootsResponse.getRoots().get(0).getId());
        // 5、列出绑定到指定资源的标签
        demo.listTagsForResource(organizationsClient, listRootsResponse.getRoots().get(0).getId());
        // 6、从指定资源中删除指定主键标签
        demo.unTagResource(organizationsClient, listRootsResponse.getRoots().get(0).getId());
        // 7、删除组织
        demo.deleteOrganization(organizationsClient);
    }

    public void createOrganization(OrganizationsClient organizationsClient) {
        CreateOrganizationRequest createOrganizationRequest = new CreateOrganizationRequest();
        try {
            CreateOrganizationResponse createOrganizationResponse =
                organizationsClient.createOrganization(createOrganizationRequest);
            logger.info(createOrganizationResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void showOrganization(OrganizationsClient organizationsClient) {
        ShowOrganizationRequest showOrganizationRequest = new ShowOrganizationRequest();
        try {
            ShowOrganizationResponse showOrganizationResponse =
                organizationsClient.showOrganization(showOrganizationRequest);
            logger.info(showOrganizationResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public ListRootsResponse listRoots(OrganizationsClient organizationsClient) {
        ListRootsRequest listRootsRequest = new ListRootsRequest();
        ListRootsResponse listRootsResponse = null;
        try {
            listRootsResponse = organizationsClient.listRoots(listRootsRequest);
            logger.info(listRootsResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
        return listRootsResponse;
    }

    public void deleteOrganization(OrganizationsClient organizationsClient) {
        DeleteOrganizationRequest deleteOrganizationRequest = new DeleteOrganizationRequest();
        try {
            DeleteOrganizationResponse deleteOrganizationResponse =
                organizationsClient.deleteOrganization(deleteOrganizationRequest);
            logger.info(deleteOrganizationResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void tagResource(OrganizationsClient organizationsClient, String resourceId) {
        if (resourceId == null || resourceId.length() == 0) {
            return;
        }
        TagResourceRequest tagResourceRequest = new TagResourceRequest()
            .withBody(new TagResourceReqBody()
                .withTags(Collections.singletonList(new TagDto().withKey("{key}").withValue("{value}")))) // key标签键的密钥标识符或名称;value与标签键关联的字符串值
            .withResourceId(resourceId);  // resourceId根、组织单元、帐号或策略的唯一标识符
        try {
            TagResourceResponse tagResourceResponse = organizationsClient.tagResource(tagResourceRequest);
            logger.info(tagResourceResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void unTagResource(OrganizationsClient organizationsClient, String resourceId) {
        if (resourceId == null || resourceId.length() == 0) {
            return;
        }
        UntagResourceRequest untagResourceRequest = new UntagResourceRequest()
            .withBody(new UntagResourceReqBody().withTagKeys(Collections.singletonList("{tag_keys}")))  // tag_keys标签列表
            .withResourceId(resourceId);  // resourceId根、组织单元、帐号或策略的唯一标识符
        try {
            UntagResourceResponse untagResourceResponse = organizationsClient.untagResource(untagResourceRequest);
            logger.info(untagResourceResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    public void listTagsForResource(OrganizationsClient organizationsClient, String resourceId) {
        if (resourceId == null || resourceId.length() == 0) {
            return;
        }
        ListTagsForResourceRequest listTagsForResourceRequest =
            new ListTagsForResourceRequest().withResourceId(resourceId);  // resourceId根、组织单元、帐号或策略的唯一标识符
        try {
            ListTagsForResourceResponse listTagsForResourceResponse =
                organizationsClient.listTagsForResource(listTagsForResourceRequest);
            logger.info(listTagsForResourceResponse.toString());
        } catch (ClientRequestException e) {
            LogError(e);
        }
    }

    private static void LogError(ClientRequestException e) {
        logger.error("HttpStatusCode: " + e.getHttpStatusCode());
        logger.error("RequestId: " + e.getRequestId());
        logger.error("ErrorCode: " + e.getErrorCode());
        logger.error("ErrorMsg: " + e.getErrorMsg());
    }
}
